#include "irq.h"
#include "time.h"
#include "sched.h"
#include "string.h"

/* exception handler */
uint64_t exception_handler[32];

/* used to init PCB */
uint32_t initial_cp0_status;

// extern void do_shell();
uint64_t time1, time2;

static void irq_timer()
{
    screen_reflush();

    /* increase global time counter */
    time_elapsed++;

    /* reset timer register */
    reset_timer();

    //time1 = get_cp0_count();

    /* sched.c to do scheduler */
    do_scheduler();

    //time2 = get_cp0_count();

    //vt100_move_cursor(1, 15);
    //printk("\rcost: %d\n", time2 - time1);
}

void interrupt_helper(uint64_t status, uint64_t cause)
{
    uint8_t IP;
    core_id = get_cpu_id();
    current_running[core_id]->type = KERNEL_PROCESS;

    IP = (uint8_t)((cause & 0x0000ff00) >> 8);
    if (IP == 0x80)
    {
        irq_timer();
    }
    current_running[core_id]->type = USER_PROCESS;
}

void other_exception_handler()
{
    vt100_move_cursor(1, 15);
    printk("> error!!! An unimplemented exception was triggered!!\n");
    uint32_t status = get_cp0_status();
    uint32_t cause = get_cp0_cause();
    uint64_t epc = get_cp0_epc();
    printk("\r> CP0_STATUS: %x\n", status);
    printk("\r> CP0_CAUSE:  %x\n", cause);
    printk("\r> CP0_EPC:    %x\n", epc);
}